package com.webshop.Dao;

import com.webshop.database.ConnPool;
import com.webshop.database.JDBCUtil;
import com.webshop.domain.Items;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PageDao {
    public List<Items> items = new ArrayList<Items>();
    public List<Items> getItems(String pclass){
        Connection conn = ConnPool.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select * from electronic_product where pclass=\""+pclass+"\"";
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()){
                Items it = new Items(rs.getString("pname"),rs.getInt("pprice"),rs.getString("pinfo"),rs.getString("pimgsrc"),rs.getString("pclass"),rs.getString("id"),rs.getString("sinfo"),rs.getString("whos"));
                items.add(it);
            }
            System.out.println(items.size());

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.release(conn,ps,rs);
        }
        return items;

    }

//    public static void main(String[] args) {
//        PageDao a = new PageDao();
//        List<Items> items= a.getItems("蔬菜");
//        for (Items i: items) {
//            System.out.println(i);
//
//        }
//    }
}
